

local HtmlUtil = require("common.HtmlUtil")

local CHAT_EVENT_STR = {
	"<A HREF=\"event:.->" ,
	"</A>",
}

local CHAT_HTML_START_SYM = "&lt;"
local CHAT_HTML_END_SYM = "&gt;"

--聊天事件


--个别不过滤的
function spMsgSolve(resultStr, spMsg, reStr, html_start, html_end, sub_start)
	local sp_start, sp_end = string.find(spMsg, reStr, html_start)

	if sp_start  == html_start then
		resultStr = resultStr .. string.sub(spMsg, html_start, sp_end)
		sub_start = sp_end + 1		
	else
		sub_start = html_end + 1
	end	
	
	return 	resultStr, sub_start
end

--过滤html
function msgDelHtml(msgStr)
	--print("msg:", msgStr)	
	
	local resultStr	= ""

	local sub_start = 1
	local sub_end = -1
	local html_start = string.find(msgStr, CHAT_HTML_START_SYM)
	local _, html_end = string.find(msgStr, CHAT_HTML_END_SYM, (html_start and html_start + 1) or 1)
	
	
	while html_start ~= nil and html_end ~= nil do
		resultStr = resultStr ..string.sub(msgStr, sub_start, html_start - 1)
					
		sub_start = html_end + 1
		
		html_start = string.find(msgStr, CHAT_HTML_START_SYM, sub_start)
		_, html_end = string.find(msgStr, CHAT_HTML_END_SYM, html_start)		
	end
	resultStr = resultStr .. string.sub(msgStr, sub_start, sub_end)
	
	--print("resultStr:", resultStr)	
	--return msgStr
	
	return resultStr
end

function Split(str, delim, maxNb)   
	if string.find(str, delim) == nil then  
		return { str } 
	end  
	if maxNb == nil or maxNb < 1 then  
		maxNb = 0    -- No limit   
	end  
	local result = {}  	
	local nb = 0  
	local startPos = 1   
	local pos1, pos2 = string.find(str, delim)
	while pos1 ~= nil and pos2 ~= nil do  
		nb = nb + 1  
		local part = string.sub(str, startPos, pos1 - 1)
		result[nb] = part   
		startPos = pos2 + 1   
		if nb == maxNb then break end  
		pos1, pos2 = string.find(str, delim, startPos + 1)
	end  
 
	if nb ~= maxNb then  
		result[nb + 1] = string.sub(str, startPos)   
	end  
	return result   
end  


--替换href事件
function hrefReplace(human, msg)
	
	local sp_start, sp_end = string.find(msg, CHAT_EVENT_STR[1])
	local result = ""
	local index_start = 1
	while sp_start ~= nil and sp_end ~= nil do
		
		local get_str = string.sub(msg, sp_start, sp_end)
		local get_str2 = Split(get_str, ":")		
		local args = Split(get_str2[2], "|")
		local event_type = args[1] + 0
		
		if #args > 0 then
			local args2 = Split(args[#args], "\"")
			
			args[#args] = args2[1]
		end		

		
		
		result = result .. string.sub(msg, index_start, sp_start - 1)
		
		--替换信息
		if HtmlUtil.TYPE_ACTION_ON_SHOW_ITEM == event_type then
			local itemGrid = getItem(human, tonumber(args[2]), tonumber(args[3]), args[4] and tonumber(args[4]))
			local itemStr = ""
			
			if itemGrid and itemGrid.id and itemGrid.id ~= 0 then
				itemStr = HtmlUtil.fontItem(itemGrid) 
			end
			
			result = result .. itemStr
			
			_, sp_end = string.find(msg, CHAT_EVENT_STR[2], sp_end)		
		else
			result = result .. string.sub(msg, sp_start, sp_end)
		end
		
		index_start = sp_end + 1
		
		sp_start, sp_end = string.find(msg, CHAT_EVENT_STR[1], index_start)		
		
	end	
	
	result = result .. string.sub(msg, index_start)

	return result
	
end

--道具所在位置
local ITEM_SOURCE_TYPE_BAG = 1
local ITEM_SOURCE_TYPE_EQUIP = 2
local ITEM_SOURCE_TYPE_STORE = 5

--展示道具
function getItem(human, type, index, itemid)
	if index < 0 then
		return 
	end

	local itemGrid
	local bagList
	local bagLen
		
	if type == ITEM_SOURCE_TYPE_BAG then
		bagList = human.db.bag
		bagLen  = human.db.bagLen
	elseif type == ITEM_SOURCE_TYPE_EQUIP then
		bagList = human.db.equip
		bagLen  = human.db.equipLen
	elseif type == ITEM_SOURCE_TYPE_STORE then
		bagList = human.db.store 
		bagLen  = human.db.storeLen
	end
	
	if bagList and index >=0 and index < bagLen then
		itemGrid = bagList[index]
		
		if itemGrid.id == 0 or itemGrid.id ~= itemid then
		  for i = 0, bagLen-1 do
				if bagList[i].id == itemid then
					itemGrid = bagList[i]
					break
				end
			end
		end
	end
	
	return itemGrid
	
end